import Vue from 'vue';
import Router from 'vue-router';
import Dashboard from './views/Dashboard.vue';
import FaceRecognition from './views/FaceRecognition.vue';
import UserManagement from './views/UserManagement.vue';
import AlarmCenter from './views/AlarmCenter.vue';
import ReviewDangerousBehavior from './views/ReviewDangerousBehavior.vue';
import ReviewDangerousPerson from './views/ReviewDangerousPerson.vue';
import UserEdit from './views/UserEdit.vue';
import UserLogin from './views/UserLogin.vue';
import UserRegister from './views/UserRegister.vue';
import UserAdd from './views/UserAdd.vue';

import DataAnalysis from './views/DataAnalysis.vue'; // 导入数据分析视图组件

Vue.use(Router);

const router = new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: UserLogin
        },
        {
            path: '/register',
            component: UserRegister
        },
        {
            path: '/dashboard',
            component: Dashboard,
            children: [
                {
                    path: '',
                    redirect: 'face-recognition'
                },
                {
                    path: 'face-recognition',
                    component: FaceRecognition
                },
                {
                    path: 'user-management',
                    name: 'userManagement',
                    component: UserManagement
                },
                {
                    path: 'alarm-center',
                    component: AlarmCenter
                },
                {
                    path: 'user-edit/:id',
                    component: UserEdit
                },
                {
                    path: 'user-add',
                    name: 'userAdd',
                    component: UserAdd
                },
                {
                    path: 'review-dangerous-behavior',
                    name: 'reviewDangerousBehavior',
                    component: ReviewDangerousBehavior
                },
                {
                    path: 'review-dangerous-person',
                    name: 'reviewDangerousPerson',
                    component: ReviewDangerousPerson
                },

                {
                    path: 'data-analysis', // 数据分析页面路由，命名路由为 dataAnalysis，指向 DataAnalysis 组件
                    name: 'dataAnalysis',
                    component: DataAnalysis
                },
            ]
        }
    ]
});

router.beforeEach((to, from, next) => {
    const isAuthenticated = !!localStorage.getItem('username');
    if (to.path !== '/login' && to.path !== '/register' && !isAuthenticated) {
        next('/login');
    } else if ((to.path === '/login' || to.path === '/register') && isAuthenticated) {
        next('/dashboard');
    } else {
        next();
    }
});

export default router;
